//
//  MSJoindViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/30.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSJoindViewController.h"
#import <AddressBook/AddressBook.h>
#import <Contacts/Contacts.h>
#import "SYContacter.h"
#import "NSString+SY.h"
#import "SYContactsHelper.h"

@interface MSJoindViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UILabel        *titleLabel;
@property (nonatomic, strong) UITableView    *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *sectionTitleArr;
@end

@implementation MSJoindViewController

- (void)initNavigationBar{
    [super initNavigationBar];
    leftBarBtn.hidden = NO;
    titleLable.text = @"已加入联系人";
}

- (void)initUI{
//    _dataArray = [@[@[@"李琦",@"李易华"],@[@"王金顺",@"王凤晨",@"王玮琦"]] mutableCopy];
//    self.sectionTitleArr = [NSMutableArray arrayWithObjects:@"L",@"W", nil];
    
    _tableView = [[UITableView alloc] initWithFrame:self.contentView.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = kDefaultViewBackgroundColor;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:_tableView];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    headerView.backgroundColor = kDefaultViewBackgroundColor;
    _tableView.tableHeaderView = headerView;
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 200, 40)];
    _titleLabel.font = HeitiFont(15);
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.text = @"已加入联系人（23）";
    [headerView addSubview:_titleLabel];
}

- (void)testDataFunc {
    [netEngine requestJoinedFriendsWithTarget:self.VctrID hudView:self.contentView];
}

- (void)updateViewController:(NSNotification *)note {
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        if (responseDic[@"JoinedFriends"]!=nil){
            NSArray *dataArray = responseDic[@"JoinedFriends"];
            [self reloadContacts:dataArray];
            _titleLabel.text = [NSString stringWithFormat:@"已加入联系人（%ld）",dataArray.count];
        }
    }
}

- (void)reloadContacts:(NSArray *)contactsTemp{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    
    UILocalizedIndexedCollation *theCollation = [UILocalizedIndexedCollation currentCollation];
    for (SYContacter *contacter in contactsTemp) {
        NSInteger sect = [theCollation sectionForObject:contacter
                                collationStringSelector:@selector(getContacterName)];
        contacter.section = sect;
    }
    
    NSInteger highSection = [[theCollation sectionTitles] count];
    NSMutableArray *sectionArrays = [NSMutableArray arrayWithCapacity:highSection];
    for (NSInteger i = 0; i <= highSection; i++) {
        NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:1];
        [sectionArrays addObject:sectionArray];
    }
    
    for (SYContacter *addressBook in contactsTemp) {
        [(NSMutableArray *)[sectionArrays objectAtIndex:addressBook.section] addObject:addressBook];
    }
    
    //getContactName 如果这个返回的是nil会有问题
    for (NSMutableArray *sectionArray in sectionArrays) {
        NSArray *sortedSection = [theCollation sortedArrayFromArray:sectionArray collationStringSelector:@selector(getContacterName)];
        [_dataArray addObject:sortedSection];
    }
    [self.tableView reloadData];
}

#pragma mark -
#pragma mark UITableViewDataSource & UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [_dataArray count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [[_dataArray objectAtIndex:section] count] ? [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section] : nil;
}

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    view.tintColor = [UIColor whiteColor];
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    header.contentView.backgroundColor=kDefaultViewBackgroundColor;
    header.textLabel.font = HeitiFont(15);
    header.textLabel.textColor = kDarkGrayColor;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return [[_dataArray objectAtIndex:section] count] ? tableView.sectionHeaderHeight : 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[_dataArray objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *kCellID = @"kCellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(12, 43.4, SCREEN_WIDTH, 0.6)];
        horizonLineView.backgroundColor = kNormalGray;
        horizonLineView.tag = 100+indexPath.row;
        [cell.contentView addSubview:horizonLineView];
    }
    
    SYContacter *addressBook = (SYContacter *)[[_dataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    
    cell.textLabel.font = HeitiFont(15);
    if ([[addressBook.name sy_trim] length] > 0) {
        cell.textLabel.text = addressBook.name;
    }
    else {
        cell.textLabel.font = [UIFont italicSystemFontOfSize:cell.textLabel.font.pointSize];
        cell.textLabel.text = @"未填写";
    }
    
    UIView *horizonLineView = [cell viewWithTag:100+indexPath.row];
    if (indexPath.row == [[_dataArray objectAtIndex:indexPath.section] count] -1) {
        horizonLineView.frame = CGRectMake(0, 43.4, SCREEN_WIDTH, 0.6);
    }
    else{
        horizonLineView.frame = CGRectMake(12, 43.4, SCREEN_WIDTH-24, 0.6);
    }
    return cell;
}


/*
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_dataArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *kCellID = @"kCellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(12, 43.4, SCREEN_WIDTH, 0.6)];
        horizonLineView.backgroundColor = kNormalGray;
        horizonLineView.tag = 100+indexPath.row;
        [cell.contentView addSubview:horizonLineView];
    }
    cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];
    cell.textLabel.font = HeitiFont(15);
    UIView *horizonLineView = [cell viewWithTag:100+indexPath.row];
    if (indexPath.row == [[_dataArray objectAtIndex:indexPath.section] count] -1) {
        horizonLineView.frame = CGRectMake(0, 43.4, SCREEN_WIDTH, 0.6);
    }
    else{
        horizonLineView.frame = CGRectMake(12, 43.4, SCREEN_WIDTH-24, 0.6);
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = kDefaultViewBackgroundColor;
    headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 30);
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH-30, 30)];
    label.textAlignment = NSTextAlignmentLeft;
    label.textColor = kDarkGrayColor;
    label.font = [UIFont fontWithName:@"STHeitiSC-Light" size:14];
    label.text = self.sectionTitleArr[section];
    [headerView addSubview:label];
    return headerView;
}
*/
@end
